package com.abe.libcore.base.screen.j;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.abe.libcore.base.screen.OnHandlerListener;
import com.abe.libquick.utils.NormalUtils;
import com.abe.libquick.utils.SpQuickHelper;

import org.jetbrains.annotations.NotNull;

import java.util.Date;
import java.util.Locale;

public abstract class AbeJActivity extends BJScreenActivity {
    private final static int appToast = 0x999;
    protected boolean isAlive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isAlive = true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isAlive = false;
    }

    @Override
    public void onConfigurationChanged(@NotNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        getResources();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration newConfig = res.getConfiguration();
        newConfig.fontScale = SpQuickHelper.fontScale();//设置标准
        res.updateConfiguration(newConfig, res.getDisplayMetrics());
        return res;
    }

    /**********************************************************************************************/
    protected Context getContext() {
        return this;
    }

    protected void gotoActivity(Class<?> act) {
        startActivity(getIntent(act));
    }

    protected void gotoActivity(Class<?> act, Bundle extras) {
        Intent intent = getIntent(act);
        intent.putExtras(extras);
        startActivity(intent);
    }

    protected void gotoActivityForResult(Class<?> act, int code) {
        startActivityForResult(getIntent(act), code);
    }

    protected void gotoActivityForResult(Class<?> act, Bundle extras, int code) {
        Intent intent = getIntent(act);
        intent.putExtras(extras);
        startActivityForResult(intent, code);
    }

    protected void setResultOK(Bundle bundle) {
        Intent intent = new Intent();
        intent.putExtras(bundle);
        setResult(RESULT_OK, intent);
        finish();
    }

    protected void setResultCancel() {
        setResult(RESULT_CANCELED);
        finish();
    }

    public Bundle bundle() {
        return getIntent().getExtras();
    }

    private Intent getIntent(Class<?> act) {
        return new Intent(this, act);
    }

    /**********************************************************************************************/
    protected void hideSoftInput() {
        InputMethodManager manager = ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE));
        if (manager == null || getCurrentFocus() == null) return;
        manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }

    public String getEmpty(String value) {
        return TextUtils.isEmpty(value) ? "" : value;
    }

    public String getText(EditText editText) {
        if (editText == null) return "";
        return editText.getText().toString().trim();
    }

    public String getText(TextView textView) {
        if (textView == null) return "";
        return textView.getText().toString().trim();
    }

    public Date stringToDate(String dateStr, String rule) {
        return NormalUtils.stringToDate(rule, dateStr);
    }

    public String dateToString(Date date, String rule) {
        return NormalUtils.dateToString(rule, date);
    }

    /**********************************************************************************************/
    protected void toast(String content) {
        if (TextUtils.isEmpty(content)) return;
        localSendMsg(appToast, content);
    }

    public void toast(int strRes) {
        toast(getResources().getString(strRes));
    }

    public void toast(String rule, Object... objects) {
        toast(String.format(Locale.CHINA, rule, objects));
    }

    protected void localSendMsg(int what, Object obj) {
        getHandler().sendMessage(getMessage(what, obj));
    }

    public void localEmptyMsg(int what) {
        getHandler().sendEmptyMessage(what);
    }

    //获取Message
    protected Message getMessage(int what, Object obj) {
        Message msg = new Message();
        msg.what = what;
        msg.obj = obj;
        return msg;
    }

    //获取Handler
    protected BaseHandler getHandler() {
        return new BaseHandler(Looper.getMainLooper());
    }

    /**
     * 是否实现接口
     *
     * @param a 类
     * @param b 接口
     */
    protected Boolean isImp(Class<?> a, Class<?> b) {
        return b.isAssignableFrom(a);
    }

    /**********************************************************************************************/
    protected class BaseHandler extends Handler {
        BaseHandler(Looper looper) {
            super(looper);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == appToast) {
                Toast.makeText(getContext(), msg.obj.toString(), Toast.LENGTH_SHORT).show();
            } else {
                if (isImp(AbeJActivity.this.getClass(), OnHandlerListener.class)) {
                    ((OnHandlerListener) AbeJActivity.this).dispatchHandleMessage(msg);
                }
            }
        }
    }
}